package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    private GeofenceMapper geofenceMapper;
    @Autowired
    private VehicleMapper vehicleMapper;
    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        //将查到的多条围栏数据VO对象装到list中,返回给前端
        List<GeofenceVO> geofenceVOList = geofenceMapper.selectGeofence(geofenceQuery);
        System.out.println("1111----"+geofenceVOList.size());
        //之前只查看围栏信息，没有统计车辆
        //遍历geofenceVOList集合，进行车辆总数和可用车辆的统计
        for (int i = 0; i < geofenceVOList.size(); i++) {
            GeofenceVO geofenceVO = geofenceVOList.get(i);
            //查询当前围栏上绑定的车辆
            VehicleQuery vehicleQuery = new VehicleQuery();
            //设置车辆的条件为当前围栏的id
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOS = vehicleMapper.selectVehicle(vehicleQuery);
            //围栏不一定有车辆，没有车辆就跳出本次循环,继续下一次循环(防止空指针异常)
            if (vehicleVOS==null)
                continue;
            //获取当前围栏的总车辆
            int totalNum = vehicleVOS.size();
            //通过空闲和占用统计可用车辆，可能以后还需要维修等状态的车辆
            int availableNum = 0; //可用车辆
            for (VehicleVO vehicleVO : vehicleVOS) {
                if(vehicleVO.getStatus().equals("1"))
                    availableNum++;
            }
            //给当前围栏对象设置可用车辆
            if(availableNum>0){
                geofenceVO.setAvailableNum(availableNum);
            }else {
                geofenceVO.setAvailableNum(0);
            }
            //给当前围栏对象设置总车辆
            geofenceVO.setTotalNum(totalNum);
            //存入geofenceVO的车辆集合中
            geofenceVO.setVehicleList(vehicleVOS);
        }
        return geofenceVOList;
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam,geofence);
        HashMap<String, String> position = geofenceParam.getPosition();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String positionStr = objectMapper.writeValueAsString(position);
            if (geofenceParam.getId()!=null){
                geofenceMapper.update(geofence);
            }else {
                geofence.setStatus("1");
                geofence.setPosition(positionStr);
                geofence.setCreateTime(new Date());
                geofenceMapper.insert(geofence);
            }
        }catch (JsonProcessingException e){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
    }

    @Override
    public void deleteGeofence(Long id) {
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        if (list!=null&&list.size()>0){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else {
            geofenceMapper.deleteById(id);
        }
    }
}